package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduComment;
import com.jf3q.common.service.EduCommentService;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.dto.CommentPageDto;
import com.jf3q.front.dto.CommentQueryDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
@Tag(name = "评论")
public class CommentController {

    @Autowired
    EduCommentService commentService;

    @Operation(summary = "分页查询评论列表")
    @PostMapping("/getCommentAll")
    public ResultVo getCommentAll(@RequestBody CommentPageDto commentPageDto){
//        if(commentPageDto.getPage()>1||commentPageDto.getPage()==null){
//            page=commentPageDto.getPage();
//        }
        LambdaQueryWrapper<EduComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduComment::getCourseId,commentPageDto.getCourseId());
        Page<EduComment> commentPage = commentService.page(new Page<>(commentPageDto.getPage(), commentPageDto.getSize()), queryWrapper);
        return ResultVo.success("",commentPage);
    }

    @Operation(summary = "添加评论")
    @PostMapping("/addComment")
    public ResultVo getAddComment(@RequestBody EduComment comment){
        if (commentService.save(comment)) {
            return ResultVo.success("添加成功",null);
        }
        return ResultVo.error("G了");
    }

    @Operation(summary = "我的评论")
    @PostMapping("/getCommentList")
    public ResultVo getMyComment(@RequestBody CommentQueryDto queryDto) {
        Page<EduComment> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        Page<EduComment> myComment = commentService.getMyComment(
                queryDto.getTitle(),
                queryDto.getContent(),
                queryDto.getMemberId(),
                queryDto.getOrderBy(),
                page
        );
        return ResultVo.success("我的评论", myComment);
    }

    @Operation(summary = "查询单个评论")
    @GetMapping("/{id}")
    public ResultVo getComment(@PathVariable String id) {
        EduComment comment = commentService.getComment(id);
        return ResultVo.success("评论详情", comment);
    }

    @Operation(summary = "修改我的评论")
    @PostMapping("/update")
    public ResultVo updateComment(@RequestBody EduComment comment) {
        LambdaQueryWrapper<EduComment> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(EduComment::getId, comment.getId());
        if (commentService.update(comment, updateWrapper)) {
            return ResultVo.success("修改成功", null);
        }
        return ResultVo.error("修改失败");
    }

    @Operation(summary = "删除我的评论")
    @DeleteMapping("/{id}")
    public ResultVo delComment(@PathVariable String id) {
        if (commentService.removeById(id)) {
            return ResultVo.success("删除成功", null);
        }
        return ResultVo.error("删除失败");
    }
}
